<?php

namespace App\Http\Controllers\Admin\Client;

use App\Http\Controllers\Controller;
use App\Http\Requests\client\CommonPriceRequest;
use App\Models\client\CommonPriceModel;
use Illuminate\Http\Request;

class CommonPrice extends Controller
{
    use CommonPriceRequest;

    /**
     * Notes:获取
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function get(Request $request) {

        $info = CommonPriceModel::query()->first();

        if($info){
            $res = [
                'tidan_price' => $info->tidan_price,
                'fendan_price' => $info->fendan_price,
            ];
        }else{
            $res = [
                'tidan_price' => '',
                'fendan_price' => '',
            ];
        }

        return $this->success($res);
    }

    /**
     * Notes:保存
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function save(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $find = CommonPriceModel::find(1);
        if($find){
            $find->tidan_price = $params['tidan_price'];
            $find->fendan_price = $params['fendan_price'];
            $find->save();
        }else{
            $model = new CommonPriceModel();
            $model->tidan_price = $params['tidan_price'];
            $model->fendan_price = $params['fendan_price'];
            $model->created_admin_id = $auth->id;
            $model->save();
        }

        return $this->success();
    }



}
